//
//  UIView+Size.m
//  MexicoLoan
//
//  Created by WXP on 2025/2/24.
//

#import "UIView+Size.h"
#import <objc/runtime.h>

static const char topToUpViewKey = '\0';

@implementation UIView (Size)

@dynamic leftCenterY;
@dynamic leftTop;
@dynamic leftBottom;
@dynamic centerXTop;
@dynamic centerXCenterY;
@dynamic centerXBottom;
@dynamic rightTop;
@dynamic rightCenterY;
@dynamic rightBottom;
@dynamic widthHeight;

#pragma mark - 运行时
-(void)setTopToUpView:(CGFloat)topToUpView
{
    objc_setAssociatedObject(self, &topToUpViewKey, [NSNumber numberWithFloat:topToUpView], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
-(CGFloat)topToUpView
{
    NSNumber * num = objc_getAssociatedObject(self, &topToUpViewKey);
    if (num == nil || ![num isKindOfClass:[NSNumber class]]) {
        return 0;
    }
    return [num floatValue];
}

#pragma mark - 组合
- (void)setLeftTop:(STRUCT_XY)leftTop{
    self.left = leftTop.horizonX;
    self.top = leftTop.verticalY;
}
- (STRUCT_XY)leftTop{
    return XY(self.left, self.top);
}
- (void)setLeftCenterY:(STRUCT_XY)leftCenterY{
    self.left =  leftCenterY.horizonX;
    self.centerY =  leftCenterY.verticalY;
}
- (STRUCT_XY)leftCenterY{
    return XY(self.left, self.centerY);
}
- (void)setLeftBottom:(STRUCT_XY)leftBottom{
    self.left =  leftBottom.horizonX;
    self.bottom =  leftBottom.verticalY;
}
- (STRUCT_XY)leftBottom{
    return XY(self.left, self.bottom);
}
- (void)setCenterXTop:(STRUCT_XY)centerXTop{
    self.centerX =  centerXTop.horizonX;
    self.top =  centerXTop.verticalY;
}
- (STRUCT_XY)centerXTop{
    return XY(self.centerX, self.top);
}
- (void)setCenterXCenterY:(STRUCT_XY)centerXCenterY{
    self.centerX =  centerXCenterY.horizonX;
    self.centerY =  centerXCenterY.verticalY;
}
- (STRUCT_XY)centerXCenterY{
    return XY(self.centerX, self.centerY);
}
- (void)setCenterXBottom:(STRUCT_XY)centerXBottom{
    self.centerX =  centerXBottom.horizonX;
    self.bottom =  centerXBottom.verticalY;
}
- (STRUCT_XY)centerXBottom{
    return XY(self.centerX, self.bottom);
}
- (void)setRightTop:(STRUCT_XY)rightTop{
    self.right =  rightTop.horizonX;
    self.top =  rightTop.verticalY;
}
- (STRUCT_XY)rightTop{
    return XY(self.right, self.top);
}
- (void)setRightCenterY:(STRUCT_XY)rightCenterY{
    self.right =  rightCenterY.horizonX;
    self.centerY =  rightCenterY.verticalY;
}
- (STRUCT_XY)rightCenterY{
    return XY(self.right, self.centerY);
}
- (void)setRightBottom:(STRUCT_XY)rightBottom{
    self.right =  rightBottom.horizonX;
    self.bottom =  rightBottom.verticalY;
}
- (STRUCT_XY)rightBottom{
    return XY(self.right, self.bottom);
}
- (void)setWidthHeight:(STRUCT_XY)widthHeight{
    self.width =  widthHeight.horizonX;
    self.height =  widthHeight.verticalY;
}
- (STRUCT_XY)widthHeight{
    return XY(self.width, self.height);
}

- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = round(x);
    self.frame = frame;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = round(y);
    self.frame = frame;
}

#pragma mark - 扩展
- (CGFloat)x
{
    return self.frame.origin.x;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}


- (CGFloat)left
{
    return self.frame.origin.x;
}

- (void)setLeft:(CGFloat)left
{
    CGRect frame = self.frame;
    frame.origin.x = round(left);
    self.frame = frame;
}

- (CGFloat)top
{
    return self.frame.origin.y;
}

- (void)setTop:(CGFloat)top
{
    CGRect frame = self.frame;
    frame.origin.y = round(top);
    self.frame = frame;
}

- (CGFloat)right {
    return self.frame.origin.x + self.frame.size.width;
}

- (void)setRight:(CGFloat)right {
    CGRect frame = self.frame;
    frame.origin.x = round(right - frame.size.width);
    self.frame = frame;
}

- (CGFloat)bottom {
    return self.frame.origin.y + self.frame.size.height;
}

- (void)setBottom:(CGFloat)bottom {
    CGRect frame = self.frame;
    frame.origin.y = round(bottom - frame.size.height);
    self.frame = frame;
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)height {
    return self.frame.size.height;
}

- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGPoint)origin {
    return self.frame.origin;
}

- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}

- (CGSize)size {
    return self.frame.size;
}

- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

#pragma mark - 高度
+ (CGFloat)getHeight:(id)model{
    return [self getHeight:model className:NSStringFromClass(self)];
}
+ (CGFloat)getHeight:(id)model className:(NSString *)strClassName{
    return [self getHeight:model className:isStr(strClassName)?strClassName:NSStringFromClass(self) selectorName:@"resetCellWithModel:"];
}
+ (CGFloat)getHeight:(id)model selectorName:(NSString *)strSelectorName{
    return [self getHeight:model className:NSStringFromClass(self) selectorName:strSelectorName];
}
+ (CGFloat)getHeight:(id)model par:(id)par className:(NSString *)strClassName selectorName:(NSString *)strSelectorName{
    return [self getHeight:model className:NSStringFromClass(self) selectorName:strSelectorName];
}
+ (CGFloat)getHeight:(id)model className:(NSString *)strClassName selectorName:(NSString *)strSelectorName{
    static NSMutableDictionary * dic;
    if (dic == nil) {
        dic = [NSMutableDictionary dictionary];
    }
    if (!isStr(strClassName)) {
        strClassName = NSStringFromClass(self);
    }
    UIView * cell = [dic objectForKey:strClassName];
    if (cell == nil) {
        cell = [NSClassFromString(strClassName) new];
        [dic setObject:cell forKey:strClassName];
    }
    [GlobalMethod performSelector:strSelectorName delegate:cell object:model isHasReturn:false];
    return cell.height;
}

@end
